package cn.qixin.passport.service.impl;

import cn.qixin.passport.service.IEmailService;
import cn.qixin.passport.web.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class EmailServiceImpl implements IEmailService {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;
    @Autowired
    RedisTemplate<String, String> redisTemplate;


    @Override
    public void sendEmail(String email) {

        String subject = "注册时的验证码";
        //获取输入的验证码
        String text = Captcha.CheckCode(4);
        //放入redis，并设置五分钟后过期  key为邮箱，value为：验证码
        redisTemplate.opsForValue().set(email,text,5, TimeUnit.MINUTES);

        //创建一封邮件
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //设置收件人
        mailMessage.setTo(email);
        //设置发件人
        mailMessage.setFrom("1625007904@qq.com");
        //设置标题
        mailMessage.setSubject(subject);
        //设置正文
        mailMessage.setText("注册齐星宠物商场需要的验证码:"+text);
        //发送邮件
        javaMailSender.send(mailMessage);
    }

}
